First let us define some basic types, such as \emph{payload, span, segment}. These fixed length byte slices enables verbose expression of fundamental units like \lstinline{segment size} or \lstinline{payload size}.

\begin{definition}[segment, payload, span, branches]\label{def:chunk-constants}
\begin{lstlisting}[language=buzz1]
// /chunk

define type segment as [32]byte    // unit for type definitions
define type payload as [:4096]byte // variable length max 4Kilobyte
define type span as uint64         // little endian binary marshalled

define function branches 
    as payload size / segment size

\end{lstlisting}
\end{definition}

Now  let's turn to the definition of \emph{address,  key} and \emph{reference}:                                             

\begin{definition}[Chunk reference]\label{def:chunk-reference}
\begin{lstlisting}[language=buzz1]
// /chunk

define type address as [segment size]byte

define type reference 
    address                   // result of bmt hash
    key if context.encryption // decryption key optional (context dependent)

\end{lstlisting}
\end{definition}

Now, define chunk as a object with span  and payload.

\begin{definition}[Content addressed chunk]\label{def:chunks}
\begin{lstlisting}[language=buzz1]
// /chunk

define type chunk
    span      // length of data span subsumed under node
    payload   // max 4096 bytes 

define function address of chunk
as
    @chunk payload bmt/hash with @chunk span 

define function create from payload
    ?over span
as
    @span = @payload length if no @span
    @chunk = chunk{ @span, @payload }
    return @chunk if no context encryption
    @key = encryption.key for @chunk 
    @chunk encrypt with @key
\end{lstlisting}
\end{definition}

Where length is the content of the length field and reference  size is the sum of size of the referencing hash value and that of the decryption key, which is currently 64, as we use 256-bit hashes and 256-bit keys.

In order to remove the padding after decryption before returning the plaintext chunk. 

\begin{definition}[Span to payload length]\label{def:span}
\begin{lstlisting}[language=buzz1]
// /chunk

define function payload.length of span
as
    while @span >= 4096 
        @span = @span + 4095
           / 4096
           * reference size
    return @span
\end{lstlisting}
\end{definition}

Finally, we can define the public API of chunks  for retrieval and storage.

\begin{definition}[Chunk API retrieval]\label{def:retrieve}
\begin{lstlisting}[language=buzz1]
// /chunk

define function retrieve reference
has api GET on "chunk/<reference>"
as 
    retrieve @reference address as chunk
        (decrypt with @reference key if @reference key)
\end{lstlisting}
\end{definition}


\begin{definition}[Chunk API: storage] \label{def:store}
\begin{lstlisting}[language=buzz1]
// /chunk


define function store payload
    ?over span 
has api POST on "chunk/(?span=<span>)"
    from payload as body
as 
    @chunk = create from @payload over @span
    reference{ @chunk address, @chunk key }
\end{lstlisting}
\end{definition}


